package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.itheima.ThreadLocalUtil;
import com.itheima.dto.MediaMaterialDto;
import com.itheima.mapper.MaterialMapper;
import com.itheima.minIo.MinIoUtil;
import com.itheima.po.MediaMaterial;
import com.itheima.service.MaterialService;
import com.itheima.vo.MediaMaterialVo;
import com.itheima.vo.PagingQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :wujunjie
 * createTime :2023-01-02 20:37
 * description :
 */
@Service
public class MaterialServiceImpl implements MaterialService {
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private MinIoUtil minIoUtil;

    @Override
    public MediaMaterialVo uploadPicture(MultipartFile multipartFile) throws IOException {

        String contentType = multipartFile.getContentType().split("/")[1];
        List<String> type=new ArrayList<>();
        type.add("jpg");
        type.add("jpeg");
        type.add("png");
        type.add("bmp");
        if (!type.contains(contentType)){
            throw new RuntimeException("图片类型不对，只能上传JPEG,JPG,PNG,BMP类型的图片");
        }
        String url = minIoUtil.uploadFile("heima-leadnews-images", IdUtil.getSnowflakeNextIdStr(),multipartFile.getInputStream(),contentType);
        MediaMaterial mediaMaterial = new MediaMaterial();
        mediaMaterial.setUrl(url);
        mediaMaterial.setId(IdUtil.getSnowflakeNextId());
        mediaMaterial.setCreateTime(LocalDateTime.now());
        mediaMaterial.setUserId(Long.parseLong(ThreadLocalUtil.getId()));
        mediaMaterial.setType(0);
        mediaMaterial.setIsCollection(0);
        int insert = materialMapper.insert(mediaMaterial);
        if (insert!=1){
            throw new RuntimeException("新增失败");
        }
        MediaMaterialVo mediaMaterialVo = new MediaMaterialVo();
        BeanUtils.copyProperties(mediaMaterial,mediaMaterialVo);
        return mediaMaterialVo;
    }

    @Override
    public PagingQueryVo<MediaMaterialVo> selectPage(MediaMaterialDto dto) {
        Integer size = dto.getSize();
        int start = (dto.getPage() - 1) * size;
        MPJLambdaWrapper<MediaMaterial> wrapper=new MPJLambdaWrapper<>();

        Integer isCollection = dto.getIsCollection();
        if (isCollection==1){
            wrapper.eq(MediaMaterial::getIsCollection,isCollection);
        }
        String last="limit "+start+","+size;
        wrapper.last(last);
        List<MediaMaterial> mediaMaterials = materialMapper.selectList(wrapper);

        if (CollectionUtils.isEmpty(mediaMaterials)){
            return null;
        }
        List<MediaMaterialVo> mediaMaterialVos = BeanUtil.copyToList(mediaMaterials, MediaMaterialVo.class);

        PagingQueryVo<MediaMaterialVo> pagingQueryVo = new PagingQueryVo<>();
        pagingQueryVo.setData(mediaMaterialVos);
        pagingQueryVo.setSize(size);
        pagingQueryVo.setCurrentPage(dto.getPage());
        MPJLambdaWrapper<MediaMaterial> countWrapper=new MPJLambdaWrapper<>();
        if (isCollection==1){
            countWrapper.eq(MediaMaterial::getIsCollection,isCollection);
        }
        Long count = materialMapper.selectCount(countWrapper);
        pagingQueryVo.setTotal(Integer.parseInt(String.valueOf(count)));
        return pagingQueryVo;
    }

    @Override
    public Boolean cancelCollection(Long id) {
        MediaMaterial mediaMaterial = new MediaMaterial();
        mediaMaterial.setId(id);
        mediaMaterial.setIsCollection(0);
        mediaMaterial.setUserId(Long.parseLong(ThreadLocalUtil.getId()));
        LambdaQueryWrapper<MediaMaterial> updateWrapper=new LambdaQueryWrapper<>();
        updateWrapper.eq(MediaMaterial::getId, mediaMaterial.getId());
        updateWrapper.eq(MediaMaterial::getUserId, mediaMaterial.getUserId());
        int update = materialMapper.update(mediaMaterial,updateWrapper);
        return update==1;
    }

    @Override
    public Boolean collect(Long id) {
        MediaMaterial mediaMaterial = new MediaMaterial();
        mediaMaterial.setId(id);
        mediaMaterial.setIsCollection(1);
        mediaMaterial.setUserId(Long.parseLong(ThreadLocalUtil.getId()));
        LambdaQueryWrapper<MediaMaterial> updateWrapper=new LambdaQueryWrapper<>();
        updateWrapper.eq(MediaMaterial::getId, mediaMaterial.getId());
        updateWrapper.eq(MediaMaterial::getUserId, mediaMaterial.getUserId());
        int update = materialMapper.update(mediaMaterial,updateWrapper);
        return update==1;
    }

    @Override
    public Boolean deletePicture(Long id) {
        MediaMaterial mediaMaterial = materialMapper.selectById(id);
        if (ObjectUtils.isEmpty(mediaMaterial)){
            throw new RuntimeException("要删除的数据不存在");
        }
        int delete = materialMapper.deleteById(id);
        return delete==1;
    }


}
